Thread: The Audio Programming Book Chapter 0 pg41 - 42 Visualising algorithms / array [-1] ?

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    The Audio Programming Book Chapter 0 pg41 - 42 Visualising algorithms / array [-1] ?

    The programming is meant to make A matrix for serial music using the 12 tone technique. I can't visualise whats happening in specfically this bit of code
    Code:
    /* create inversion in column 1 */
        for(m = 1; m < 12; m++)
            series[m][0] = mod12(series[m-1][0] + series[0][m-1]
                                 - series[0][m]);
    I'm expecting something like this
    for (m = 1; m < 12; m ++)
    series[m][0] = [0][-1]
    series[m][0] = [1][-1]
    series[m][0] = [2][-1]

    and so on...

    is it saying take the result of mod12(series[x][x]) and put it into series[x][x]?

    is series[x][x] a single value or two?


    Code:
    /* The first row of the matrix will receive the original twelve notes, from left to right.
     The inversion of the series will fill the first column of the matrix, from top to bottom.
     To complete the matrix, the other rows will receive transposed versions of the original
     series, each one starting with the note in the first column. */
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int mod12(int note) {
        while(note >= 12) note -= 12;
        while(note < 0) note += 12;
        return note;
    }
    
    
    int main(int argc, char** argv)
    {
        int series[12][12], offset;
        int n, m, i;
        char* table[12]={"C","Db","D","Eb",
                        "E","F","Gb","G",
                        "Ab","A","Bb","B"};
        if(argc != 13) {
            printf("usage: %s note1 note2 ... note12\n", argv[0]);
            return -1;
        }
        /* loop until all available notes are entered*/
        for(n = 0; n < 12; n++)
            series[0][n] = mod12(atoi(argv[n+1]));
        
        /* create inversion in column 1 */
        for(m = 1; m < 12; m++)
            series[m][0] = mod12(series[m-1][0] + series[0][m-1]
                                 - series[0][m]);
        
        /*create all transposition */
        for(m = 1; m < 12; m++)
            for(n = 1; m < 12; m++)
                series[m][n] = mod12(series[0][n] + series[m][0]
                                     - series[0][0]);
        
        for(m = 0; m < 12; m++){
            /* print the pitch classes, row by row using the
                 translation table */
            for(n = 0; n < 12; n++) printf(" %s ", table[series[m][n]]);
            printf("\n");
        }
    
    
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't really know music, so I'm not sure how much I can help.

    But your statement resolves like this:

    series[m][0] = mod12(series[m-1][0] + series[0][m-1] - series[0][m]);
    series[1][0] = mod12(series[(1)-1][0] + series[0][(1)-1] - series[0][1]);
    series[1][0] = mod12(series[0][0] + series[0][0] - series[0][1]);

    As m grows, it depends on the previous row's first column, and the first row's mth and m-1 column.
    Last edited by whiteflags; 01-10-2018 at 08:12 PM.

  3. #3
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    but:
    series[1][0] = mod12(series[0][0] + series[0][0] - series[0][1]);
    means that series[1][0] -= series [0][1];
    whats happens where [0] - [1]?

    So far I can only see it means [0] - [1] = [-1], which wouldnt be allowed in this case?

    if i do this it prints a negative version of the number 0 - 11 ?

    Code:
    
    
    Code:
    for(n = 0; n < 12; n++)
            series[0][n] = mod12(n);
        
           int debug = series[0][0] - series[0][11];
        
        printf(" %i : \n", debug);
    
    
    Last edited by Fauveboy; 01-10-2018 at 08:30 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There is a difference between the subscript n itself going negative, which it never does, and the note going negative. Sometimes the note will go negative, which is why you mod12 it. I don't think you completely understand which numbers are doing what in the program.

    This should show you.
    Code:
    int note;
    for (m = 1; m < 12; m++) {
       printf("m=%d, m-1=%d\n", m, m-1);
       note = series[m-1][0] + series[0][m-1] - series[0][m];
       printf("note before mod=%d\n", note);
       series[m][0] = mod12(note);
       printf("note after mod=%d\n", series[m][0]);
    }

  5. #5
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    These are some results here:

    m=1, m-1=0


    note before mod=0


    note after mod=0


    m=2, m-1=1


    note before mod=0


    note after mod=0


    m=3, m-1=2


    note before mod=0


    note after mod=0


    m=4, m-1=3


    note before mod=0


    note after mod=0


    m=5, m-1=4


    note before mod=0


    note after mod=0


    m=6, m-1=5


    note before mod=0


    note after mod=0


    m=7, m-1=6


    note before mod=0


    note after mod=0


    m=8, m-1=7


    note before mod=0


    note after mod=0


    m=9, m-1=8


    note before mod=0


    note after mod=0


    m=10, m-1=9


    note before mod=0


    note after mod=0


    m=11, m-1=10


    note before mod=0


    note after mod=0

    These are the results when :


    for(n = 0; n < 12; n++)
    series[0][n] = mod12(n);

    Joels-MacBook-Pro:APB joel$ ./chap0 Db C D Eb E F Gb G Ab A Bb B
    m=1, m-1=0
    note before mod=-1
    note after mod=11
    m=2, m-1=1
    note before mod=10
    note after mod=10
    m=3, m-1=2
    note before mod=9
    note after mod=9
    m=4, m-1=3
    note before mod=8
    note after mod=8
    m=5, m-1=4
    note before mod=7
    note after mod=7
    m=6, m-1=5
    note before mod=6
    note after mod=6
    m=7, m-1=6
    note before mod=5
    note after mod=5
    m=8, m-1=7
    note before mod=4
    note after mod=4
    m=9, m-1=8
    note before mod=3
    note after mod=3
    m=10, m-1=9
    note before mod=2
    note after mod=2
    m=11, m-1=10
    note before mod=1
    note after mod=1

    I'm confused it looks like it does go into minuses, im not sure where its getting that value from. I would've presumed it is a bit a memory that isnt there?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    The double loop that creates the transpositions has an error.
    Code:
    for(m = 1; m < 12; m++)
        for(n = 1; m < 12; m++) //should be: for(n = 1; n < 12; n++)
    It looks like the input is supposed to be numbers from 0 to 11 (or if they're out of that range they'll be mod12'ed into it).

    So try something like:
    ./chap0 4 3 5 6 7 8 9 10 11 0 1 2
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    the programme works with now the correct arg inputs. under /*create inversion in column 1*/ I just don't understand why series[0][-1] = -1 before using mod12() = 11.
    but series[0][-1] is invalid ? surely this should be garbage values. A value is never put there because the element [-1] doesnt exist?

  8. #8
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    I think this code proves that. whats the difference here between this code and: series[m-1][0] + series[0][m-1] - series[0][m]?

    the compiler warning:
    chap0_array.c:18:36: warning: array index -1 is before the beginning of the array
    [-Warray-bounds]
    printf(" print array -1 : %i \n", array[-1]);


    the program output:

    Joels-MacBook-Pro:APB joel$ ./chap0_array
    print array: 0
    print array: 1
    print array: 2
    print array: 3
    print array: 4
    print array: 5
    print array: 6
    print array: 7
    print array: 8
    print array: 9
    print array: 10
    print array -1 : 5
    Abort trap: 6
    Last edited by Fauveboy; 01-13-2018 at 10:28 AM.

  9. #9
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    ah...arithmetic isn't moving through the array elements it's pointing to the actual values.

  10. #10
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    However, its seems that on the left side of the "=" with series[m][0], only the elements are adjusted wheres on the right side of the "=" the values are influenced is that right? What makes for this change in behaviour ?

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Fauveboy View Post
    However, its seems that on the left side of the "=" with series[m][0], only the elements are adjusted wheres on the right side of the "=" the values are influenced is that right? What makes for this change in behaviour ?
    x=2;
    This is an assignment statement. Like other more complicated assignment statements, what changes is on the left side.

    We can say the same about this statement:
    series[m][0] = mod12(series[m-1][0] + series[0][m-1] - series[0][m]);
    The return value of mod12 is what modifies series[m][0]. If we look more carefully at the statement, we see that mod12 has a complicated argument, which must be evaluated before mod12 is called.

    So let's just run through an example. We can just use the numbers that john.c posted. These numbers are in the first row of the series.
    ./chap0 4 3 5 6 7 8 9 10 11 0 1 2

    If you notice, the loop starts off at 1, so the lowest m could be is 1.
    series[1][0] = mod12(series[0][0] + series[0][0] - series[0][1]);
    series[1][0] = mod12(4 + 4 - 3);
    series[1][0] = mod12(5);
    series[1][0] = 5;

    I mean it might not be the best example, because mod12 doesn't do much.

    But, as I said, there is a difference between a small expression like m-1 being used as an array subscript, and the actual values in the array. The reason m-1 is never -1, is because m is never supposed to be zero.
    Last edited by whiteflags; 01-13-2018 at 01:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmask (example from The Audio Programming Book pg 22)
    By Fauveboy in forum C Programming
    Replies: 2
    Last Post: 01-02-2018, 11:10 PM
  2. Question on Chapter 7 sample in the book
    By CMaker3 in forum C++ Programming
    Replies: 1
    Last Post: 07-31-2013, 05:39 AM
  3. A good book on algorithms ?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 03:58 PM
  4. A good book on algorithms for C
    By pritesh in forum C Programming
    Replies: 5
    Last Post: 11-16-2001, 02:12 AM
  5. audio effect algorithms in c
    By jam1e1 in forum C Programming
    Replies: 4
    Last Post: 10-13-2001, 06:36 AM

Tags for this Thread